package edu.avans.hartigehap.domain;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.persistence.*;

import org.joda.time.DateTime;


@Entity
@Table(name = "DISCOUNTS")
public abstract class Discount implements Serializable {
	protected Restaurant restaurant;
	//auto incrementing id
	protected Integer id;
	//if this is the one active discount it will be true
	protected Boolean active;
	//create a date = right now
	protected double lValue;
	protected Date date;
	//bill needed for the relation
	private Collection<Bill> bills = new ArrayList<Bill>();
	
	/**
	 * A constructor for creating inactive discounts
	 * @param active
	 */
	public Discount()
	{}
	
	/**
	 * A constructor for creating active discounts
	 * @param active
	 */
	public Discount(Boolean active, Date date, double lValue){
		setActive(active);
		setValue(lValue);
		setDiscountActive(date, active);
	}
	
	//getter and setter for attribute 'id'
    @Id
    @GeneratedValue
    @Column(name = "DISCOUNT_ID")
    public Integer getId() {
       return id;
    }
    public void setId(Integer id) {
 	   this.id = id;
    }
    
    //getter and setter for attribute 'active'
	@Column (name = "ACTIVE")
	public Boolean getActive(){
		return active;
	}
	public void setActive(Boolean active)
	{
		this.active = active;
	}
	
	//method to activate the discount for x days
	public void setDiscountActive(Date date, Boolean active){
		if(active)
		{
			this.date = date;
		}
		//set all others inactive
		setActive(active);
	}
	
	@Column (name = "DATE")
	public Date getDate()
	{
		return date;
	}
	
	public void setDate(Date date)
	{
		this.date = date;
	}
	
	//getter and setter for attribute 'value'
	@Column (name = "VALUE", nullable = true)
	public double getValue(){
		return lValue;
	}
	public void setValue(double value){
		this.lValue = value;
	}

	//relation with a set of Bills
	@OneToMany(cascade = javax.persistence.CascadeType.ALL, mappedBy = "discount")
    public Collection<Bill> getBills() {
       return bills;
    }
    public void setBills(Collection<Bill> bills) {
       this.bills = bills;
    }
    
    //used for creating test data
	@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
	public Restaurant getRestaurant() {
		return restaurant;
	}
	public void setRestaurant(Restaurant restaurant) {
		this.restaurant = restaurant;
	}
	
    //Method for the discount calculation
    @Transient
    public abstract double calculate(double newprice);
}